import {
    searchList,
    hotSearch,
    shopCarAdd,
    getShopCarList
} from "../../utils/config";
import store from "../../utils/factory";

let that = null;
//获取应用实例
const app = getApp();

Page({
    data: {
        inputVal: "",
        historyList: [],
        hotList: [],
        note: [],
        page: 1,
        searchView: true,
        count: 0,
        finish: false,
    },
    //事件处理函数
    inputTyping: function (e) {
        let val = e.detail.value;
        that.setData({
            inputVal: val
        });
        if (!val) {
            that.setData({
                searchView: true
            });
        }
    },
    onLoad: function () {
        that = this;
    },
    onShow() {
        that.setData({
            historyList: store.get("historyList") || [],
            finish: false,
            searchView: true
        });
        hotSearch().then(res => {
            that.setData({
                hotList: res.rows
            });
        });
        this.getTotal()
    },
    goShopCarList() {
        wx.switchTab({
            url: "../shopCar/shopCar"
        });
    },
    onReachBottom() {
        console.log("bottom")
        if (that.data.finish) {
            return false
        }
        that.setData({
            page: that.data.page + 1
        })
        that.search(this.data.inputVal);
    },
    getTotal() {
        getShopCarList().then(res => {
            let req = res.data;
            this.setData({
                count: req.quantity || 0,
            })
        })
    },
    confirmInput(e) {
        let val = this.data.inputVal;
        console.log("confirm", val);
        if (!val) {
            return false;
        }
        let list = store.get("historyList") || [];
        list.push({
            name: val
        });
        store.set("historyList", list);
        this.setData({
            finish: false,
            page: 1,
            note: []
        })
        that.search(val);
    },
    searchHot(e) {
        let name = e.currentTarget.dataset.name;
        let list = store.get("historyList") || [];
        var item = list.find((v) => {
            return v.name == name;
        });
        if (!item) {
            list.push({
                name: name
            });
            store.set("historyList", list);
        }
        that.setData({
            inputVal: name,
            page: 1,
            finish: false,
            note: []
        })
        that.search(name);
    },
    search(value) {
        searchList({
            page: this.data.page,
            limit: 10,
            name: value
        }).then(res => {
            let req = res.data;
            req.map(item => {
                item.progress = parseInt(item.nowQuantity / item.minQuantity);
                if (item.progress > 1) {
                    item.progress = 1
                }
            })
            if (req && req.length < 10) {
                that.setData({
                    finish: true
                });
            } else {
                that.setData({
                    finish: false
                });
            }
            let a = [...this.data.note, ...req]
            that.setData({
                searchView: false,
                note: a,
            });
            console.log("note", this.data.note)
        });
    },
    addShopCar: function (e) {
        if (!store.get("userInfo")) {
            wx.navigateTo({
                url: '../autoLogin/autoLogin' // 页面 B
            })
            return false
        }
        console.log(e)
        let detailObj = e.currentTarget.dataset.obj
        if (!detailObj) {
            wx.showToast({
                title: "该商品不存在",
                icon: "none"
            })
            return false
        }
        if (detailObj.saleOut) {
            wx.showToast({
                title: "该商品已经售罄",
                icon: "none"
            })
            return false
        }
        shopCarAdd({
            category: detailObj.id,
            quantity: 1
        }).then(res => {
            wx.showToast({
                title: '添加购物车成功',
                icon: 'none',
                duration: 1000
            })
            this.setData({
                count: parseInt(this.data.count) + 1
            })
        })
    },
    focusOn(e) {
        if (that.data.note.length > 0) {
            return false
        }
        that.setData({
            searchView: true
        });
        console.log("focus", e);
    },
    goDetail(e) {
        const id = e.currentTarget.dataset.id;
        console.log(id);
        wx.navigateTo({
            url: `../logs/logs?id=${id}`
        });
    },
    clear() {
        store.remove("historyList");
        that.setData({
            historyList: []
        })
    }
});